package com.dao.impl;

import com.dao.TableDao;
import com.util.Error;
import com.persistence.TablePersistence;
import com.models.Table;

import java.util.ArrayList;

/**
 * Created by TimeLiar on 2015/9/24.
 * Biu~~Biu~~~Biu~~~~
 */
public class TableDaoImpl implements TableDao {
    @Override
    public void add(Table table) {
        synchronized (TablePersistence.class){
            try {
                TablePersistence.createTable(table);
                System.out.println("add table '" + table.getName() + "' success");
            } catch (com.util.Error error) {
                System.out.println(error.getMessage());
            }
        }
    }

    @Override
    public void delete(String tableName) {
        synchronized (TablePersistence.class){
            if(TablePersistence.removeTable(tableName)){
                System.out.println("delete table '" + tableName + "' success");
            }
        }
    }

    @Override
    public void update(Table table) {
        synchronized (TablePersistence.class){
            try {
                TablePersistence.updateTable(table);
            } catch (Error error) {
                System.out.println(error.getMessage());
            }
        }
    }

    @Override
    public Table getTable(String table) {
        return TablePersistence.getTable(table);
    }

    @Override
    public ArrayList<Table> all() {
        return TablePersistence.allTables();
    }
}
